﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using Autorent.DAL;

namespace Autorent.Service
{


    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class AutorentMechServ
    {
        [OperationContract]
        public void AddDateReturn(DateTime dateReturn, int carId)
        {
            AutoRentEntities ctx = new AutoRentEntities();
            //update the autoOrder record for the selected car
            ctx.AutoOrders.FirstOrDefault(
                a => a.Auto_ID == carId && a.AutoOrderReturningDT == null).AutoOrderReturningDT = dateReturn;
            ctx.SaveChanges();
        }


        [OperationContract]
        public void AddCarDamage(AutoDamageContract autoDamage, int carId)
        {
            AutoRentEntities ctx = new AutoRentEntities();
            //get the order for the selected car using car number and autoDamage params
            decimal autoOrderId = ctx.AutoOrders.FirstOrDefault(a => a.Auto_ID == carId && a.AutoDamages == null).AutoOrder_ID;
            AutoDamage damage = new AutoDamage()
            {
                AutoDamageDescription = autoDamage.DamageDescription,
                AutoDamageEstimatedCost = autoDamage.DamageRepairCost,
                AutoOrder_ID = autoOrderId
            };
            ctx.AddToAutoDamages(damage);
            ctx.SaveChanges();
        }


        [OperationContract]
        public string GetCarName(int carId)
        {
            AutoRentEntities ctx = new AutoRentEntities();
            return ctx.Autoes.FirstOrDefault(a => a.Auto_ID == carId).AutoModel.ToString();
        }

        [OperationContract]
        public void SetGasQuantity(int gasQuantity, int carId)
        {
            AutoRentEntities ctx = new AutoRentEntities();
            ctx.Autoes.FirstOrDefault(a => a.Auto_ID == carId).AutoPetrolTankFilled = gasQuantity;
            ctx.SaveChanges();
        }
    }


    [DataContract]
    public class AutoDamageContract
    {
        private string _damageDescription;
        private int _damageRepairCost;

        [DataMember]
        public string DamageDescription
        {
            get { return this._damageDescription; }
            set { this._damageDescription = value; }
        }

        [DataMember]
        public int DamageRepairCost
        {
            get { return this._damageRepairCost; }
            set { this._damageRepairCost = value; }
        }
    }
}
